#ifndef FONT_H_
#define FONT_H_

#include "File.h"
#include "Video.h"
#include "Point2d.h"
#include "Color.h"
#include <gl.h>
#include <SDL_ttf.h>

namespace Lame3d
{	
	class Font
	{
		protected:			
			class Glyph
			{
				public:
					Glyph();
					~Glyph();
					
					void Init(
						const Point2d &uLeft,
						const Point2d &lRight,
						const Point2d &dims,
						float advance,
						float drop);
					
					void Render();
					void Advance();
					
					float GetAdvance() const;
										
				protected:
					GLuint list;
					float advance;
			};
		
		public:
			Font(const File &f, const DisplayManager &dM, const Color& c, int size=12);
			~Font();
			
			class Handle
			{
				public:
					~Handle();
					
					void NewLine();
					float Width(const std::string &message) const;
					float LineHeight() const;
					
					// Better printing functions; wrap is assumed
					void Print(const std::string &message, const Point2d &dims, int index=0);
					void PrintEnd(const std::string &message, const Point2d &dims);					
					
					Handle& operator=(const Handle &h);
					
				protected:
					Handle(Font &f);
					
					Font *font;
					
					float print(char c);
					void bind();
					void unbind();
					void printSmart(const std::string &message, float width);
					
				friend class Font;
				friend class FormattedString;
			};
			
			Handle GetHandle();
			
		protected:
			File file;
			int refCount;
			Glyph *glyphs; // 94 glyphs.
			int fontSize;
			float lineHeight;
			GLuint texture;
			
			const DisplayManager &displayManager;
			const Color color;
			
			void load();
			void unload();
			void makeGlyph(char c, TTF_Font *ft);
			
		friend class Handle;
		friend class FormattedString;
	};
	
	class FormattedString
	{
		public:
			FormattedString(const std::string &s, 
				Font::Handle &h, 
				float width=1, 
				float height=1, 
				int cursor=-1, 
				bool wrap=true);
			~FormattedString();
			
			const std::string& String() const;
			
			float Width() const;
			float Height() const;
			int Lines() const;
			
			void Render();
			
		protected:
			std::string string;
			
			float width;
			float height;
			int lines;
			
			int leftCursor;
			int rightCursor;
			
			Font::Handle &handle;
			
			void format(float w, float h, int c, bool wrap);
			
		friend class Font::Handle;
	};
}

#endif /*FONT_H_*/
